var Pagination = require("COMMON/modules/pagination-x/v1.0");
var ParseTemplate = ZFT.Util.ParseTemplate;
var cData = require("COMMON/js/config.province.city.data");
var Template = {
    cntbox: ParseTemplate(require("./tpl/cntbox.xtpl")),
    cnlist: ParseTemplate(require("./tpl/cntlist.xtpl"))
}
var Main = ZFT.Util.Class({
    container: $("body"),
    init: function () {
        var _this = this;
        _this.loadCntBox();
        _this.initPagination();
    },
    EVENTS: {
        "mousedown .filterBtn": "onFilterClick",
        "keyup .filterInp": "onFilterEnter",
    },
    //初始化分页
    initPagination: function () {
        var _this = this;
        var pagination = _this.pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true
        });

        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                _this.loadList(toPage);
        });
        _this.loadList(1);
    },
    loadCntBox: function () {
        ZFT.Util.Ajax("/r/building_getTrustSummary", {
            type: "POST",
            dataType: "json",
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.cntbox({ data: res.data });
                    $("#cntBox").html(html);
                } else {
                    ZFT.Util.STip("fail", res.msg, 4000);
                }
            }
        })
    },
    //获取楼宇列表
    loadList: function (toPage) {
        var _this = this;
        var tbody = $("#tbody");
        var page = $(".paginationWrap");
        var page_size = $("#paginationWrap").attr("data-size");
        var param = {
            page: toPage,
            page_size: page_size ? page_size : 15
        }
        ZFT.Util.Ajax("/r/Building_getBuildingSummary", {
            type: "POST",
            dataType: "json",
            params: param,
            loading: function () {
                tbody.html('<tr><td colspan="12">加载中,请稍后...</td></tr>');
                page.hide();
            },
            success: function (res) {
                var list = res.data.list
                if (res.code == 200) {
                    if (!list) {
                        tbody.html('<tr><td colspan="12">暂无数据</td></tr>');
                        return false;
                    } else {
                         for (var i = 0, len = list.length; i < len; i++) {
                                var item = list[i];
                                var prov = item.province;
                                var city = item.city;
                                var area = _this.getLocationName(prov, city);
                                item["city_area"] = area;
                            }
                        var html = Template.cnlist({ data: list });
                        tbody.html(html);
                        _this.pagination.render({ current: toPage, toPage: toPage, total: res.data.total });
                    }

                } else if (res.code == 102) {
                    var relogin = '<tr><td colspan="12"><a href="index/logOut">未登录,请重新登录</a></td></tr>'
                    tbody.html(relogin);
                } else {
                    var html = '<tr><td colspan="12">' + res.msg + '</td></tr>'
                    tbody.html(html);
                }
            },
            error: function () {
                tbody.html('<tr><td colspan="12">请求出错,请重试!</td></tr>');
            }
        })
    },

    //通过区域码获取对应名称
    getLocationName: function (prov, cityId) {
        var province = cData[prov];
        var prov_name = province.name;
        var citys = province.city;
        var city_name = "";
        for (var i = 0; i < citys.length; i++) {
            var city = citys[i];
            var city_id = city.id;
            if (city_id == cityId) {
                city_name = city.name;
                break;
            }
        }
        return prov_name + " " + city_name;
    },

    //设置每页显示多少条
    onFilter: function (filVal) {
        var _this = this;
        if (!filVal || !(/^([1-9]\d?|100)$/.test(filVal))) return false;
        filVal = $.trim(filVal);
        $("#paginationWrap").attr("data-size", filVal);
        _this.loadList(1);
    },
    onFilterClick: function (e) {
        var _this = this;
        var tarBtn = $(e.currentTarget);
        var filVal = tarBtn.siblings(".filterInp").val();
        _this.onFilter(filVal);
    },
    onFilterEnter: function (e) {
        var _this = this;
        var keyCode = e.keyCode;
        var tarInp = $(e.currentTarget);
        var tarInpVal = tarInp.val();
        if (keyCode != 13) return false;
        _this.onFilter(tarInpVal);
    },
});

$(function () {
    new Main();
})